package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.Gwat900Mapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwaklT;
import cn.e4j.modular.crm.entity.GwalT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class Gwat900Service{

    @Autowired
    private Gwat900Mapper gwat900Mapper;

    /**
     * 获取客户列表
     * <p>
     * 1.最终客户、2.经销商
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/10 13:53
     */
    public List<Dto> getGwakList(Page<Object>page, Dto inDto) {
        return gwat900Mapper.getGwakPage(page,inDto);
    }


    /**
     * 客户信息保存
     */
    @Transactional
    public void updateGwak(GwakT gwak_tPO, GwaklT gwakl_tPO) {

        UpdateWrapper<GwakT> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("GWAKENT",gwak_tPO.getGWAKENT());
        wrapper1.eq("GWAK001",gwak_tPO.getGWAK001());
        gwak_tPO.update(wrapper1);

        UpdateWrapper<GwaklT> wrapper2 = new UpdateWrapper<>();
        wrapper2.eq("GWAKLENT",gwakl_tPO.getGWAKLENT());
        wrapper2.eq("GWAKL001",gwakl_tPO.getGWAKL001());
        gwakl_tPO.update(wrapper2);

    }

    /**
     * 获取客户联系人列表
     *
     * @param inDto
     * @return
     * @author wenchuan.li on 2018/07/10 13:53
     */
    public List<Dto> getGwalList(Page<Object>page,Dto inDto) {
        return gwat900Mapper.getGwalPage(page,inDto);
    }

    /*
     * 获取区域
     */
    public List<Dto> getAreas(Dto inDto) {

        return gwat900Mapper.getAreas(inDto);
    }

    /**
     * 根据地址类型获取客户数量
     */
    public int checkType(Dto inDto) {
        GwalT gwalT = CommonUtils.toJavaClass(inDto,GwalT.class);
        QueryWrapper<GwalT> gwalTQueryWrapper = new QueryWrapper<>();

        inDto.forEach((k,v)->{
            if (k.startsWith("GWAL") && (v!=null && v.equals("")))
                gwalTQueryWrapper.eq(k,v);
        });
        return gwalT.selectCount(gwalTQueryWrapper);
    }

    /**
     * 客户信息保存
     */
    @Transactional
    public void saveGwal(GwakT gwak_tPO, GwalT gwal_tPO, String type) {
        if (type.equals("add")) {
            Dto dto = gwat900Mapper.getMax002(gwal_tPO);
            if (ToolUtil.isEmpty(dto)) {
                gwal_tPO.setGWAL002("1");
            } else {
                gwal_tPO.setGWAL002(dto.getString("COUNT"));
            }
            gwal_tPO.insert();
        } else if (type.equals("edit")) {
            //WHERE  gwalent = #{gwalent} AND gwal001 = #{gwal001} AND gwal002 = #{gwal002}
            UpdateWrapper<GwalT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWALENT",gwal_tPO.getGWALENT());
            wrapper.eq("GWAL001",gwal_tPO.getGWAL001());
            wrapper.eq("GWAL002",gwal_tPO.getGWAL002());
            gwal_tPO.update(wrapper);
        }
        if (gwal_tPO.getGWAL003().equals("Y")) {
            //WHERE  gwakent = #{gwakent} AND gwak001 = #{gwak001}
            UpdateWrapper<GwakT> wrapper = new UpdateWrapper<>();
            wrapper.eq("GWAKENT",gwak_tPO.getGWAKENT());
            wrapper.eq("GWAK001",gwak_tPO.getGWAK001());
            gwak_tPO.update(wrapper);
        }
    }

    @Transactional
    public void delGwal(Dto inDto) {
        QueryWrapper<GwalT> wrapper = new QueryWrapper<>();
        wrapper.eq("GWALENT",inDto.getString("GWALENT")).
                eq("GWAL001",inDto.getString("GWAL001")).
                eq("GWAL002",inDto.getString("GWAL002"));
        new GwalT().delete(wrapper);
    }

}
